# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import os
import time


def show_version_log():
    print("*********************************************************************************************")
    print(" Copyright (c) 2015 XXX Technology Co., Ltd. ")
    print(" All rights reserved.")
    print(" @File       :", __file__)
    print(" @Brief      : Auto Migrate srcIP:XXX.git to dstIP:29418/XXX.git")
    print(" ")
    print(" @Note       : //")
    print(" ")
    print(" @Date       :", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    print(" @Author     : Mike Ross")
    print(" @Version    : 1.0.1")
    print(" History     : ")
    print(" ")
    print("                 1. Setup script @2020/11/19 --lu ming liang")
    print("                 2. Release script @2020/11/23 --lu ming liang")
    print("*********************************************************************************************")
    print(" ")
    print(" ")
    print(" ")


def imi_exit_loop():
    user_action = int(input('if you want to finish ,input number 9 to exit ...'))
    if user_action == 9:
        return 0
    else:
        return 11


def main_loop():
    os.system('git clone srcIP:XXX.git')
    os.chdir('c:/Users/Desktop/Administrator/XXX')
    os.system('git branch -r > c:/Users/Administrator/Desktop/branch.txt')
    with open('c:/Users/Administrator/Desktop/branch.txt', 'r', encoding="utf-8") as repo_branch_files:
        branch_migrate_count = 0
        os.system('git pull --all')
        os.system('git remote set-url origin "dstIP/XXX.git"')
        for line in repo_branch_files:
            print("******************************************************************************")
            print("[Current branch ->", line.strip('\n'), "]", "Index :", branch_migrate_count)
            print("******************************************************************************")
            user_input = input("Continue to Migrate Branch (y/N)??")
            if user_input == 'Y' or user_input == 'y' or user_input == 'yes' or user_input == 'YES':
                # branch.txt line value for example : [space]origin/branchname[line break(\n)]
                line_remove_line_break = line.strip('\n')
                line_remove_space_and_line_break = line_remove_line_break.strip(' ')
                line_remove_space_and_line_break_and_origin = line_remove_space_and_line_break.split('/', 2)[1]
                # Step 1, Prepare to create local branch and remote branch name
                branch_name_local = line.split('/', 2)[1].strip('\n')
                branch_name_local = branch_name_local.strip(' ')
                branch_name_remote = "remotes/" + line.strip(' ')
                branch_name_remote = branch_name_remote.strip('\n')
                # Step 2, git checkout branch
                checkout_branch_cmd = '{local} {remote}'.format(local=branch_name_local, remote=branch_name_remote)
                os.system("git checkout -b %s" % checkout_branch_cmd)
                # Step 3, git push local branch to remote Server(dstIP/XXX.git/Branch_name)
                push_branch_cmd = '{local}:{remote}'.format(local=branch_name_local,
                                                            remote=line_remove_space_and_line_break_and_origin)
                os.system('git push origin %s' % push_branch_cmd)
                # os.system("git push origin --all")
                # os.system("git push origin --tags")
            else:
                print("Unknown instruction !!! ")
            # for use to separate the log window
            print(" ")
            print(" ")
            print(" ")
            branch_migrate_count = branch_migrate_count + 1

    # Step 4, git push origin --tags (use Gerrit forge commiter style)
    os.system('git push origin --tags')
    print("******************************************************************************")
    print("It's Done! , Migrate branch number :", branch_migrate_count)
    print("******************************************************************************")
    repo_branch_files.close()
    while imi_exit_loop() > 0:
        print('please input number : ')

    print('Exit main loop ...')


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    show_version_log()
    main_loop()


# See PyCharm help at https://www.jetbrains.com/help/pycharm/
